package com.ruoyi.attachment.service.impl;

import com.ruoyi.attachment.domain.Attachment;
import com.ruoyi.attachment.mapper.AttachmentMapper;
import com.ruoyi.attachment.service.IAttachmentService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * 附件Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-18
 */
@Service
public class AttachmentServiceImpl implements IAttachmentService
{
    @Resource
    private AttachmentMapper attachmentMapper;

    /**
     * 查询附件
     *
     * @param attachmentId 附件主键
     * @return 附件
     */
    @Override
    public Attachment selectAttachmentByAttachmentId(Long attachmentId)
    {
        return attachmentMapper.selectAttachmentByAttachmentId(attachmentId);
    }

    /**
     * 查询附件列表
     *
     * @param attachment 附件
     * @return 附件
     */
    @Override
    public List<Attachment> selectAttachmentList(Attachment attachment)
    {
        return attachmentMapper.selectAttachmentList(attachment);
    }

    /**
     * 新增附件
     *
     * @param attachment 附件
     * @return 结果
     */
    @Override
    public int insertAttachment(Attachment attachment)
    {
        attachment.setCreateTime(DateUtils.getNowDate());
        return attachmentMapper.insertAttachment(attachment);
    }

    /**
     * 修改附件
     *
     * @param attachment 附件
     * @return 结果
     */
    @Override
    public int updateAttachment(Attachment attachment)
    {
        return attachmentMapper.updateAttachment(attachment);
    }

    /**
     * 批量删除附件
     *
     * @param attachmentIds 需要删除的附件主键
     * @return 结果
     */
    @Override
    public int deleteAttachmentByAttachmentIds(Long[] attachmentIds)
    {
        return attachmentMapper.deleteAttachmentByAttachmentIds(attachmentIds);
    }

    /**
     * 删除附件信息
     *
     * @param attachmentId 附件主键
     * @return 结果
     */
    @Override
    public int deleteAttachmentByAttachmentId(Long attachmentId)
    {
        return attachmentMapper.deleteAttachmentByAttachmentId(attachmentId);
    }

    /**
     * 删除服务器附件
     * @param filePath
     */
    @Override
    public void deleteServerFile(String filePath){
        //基础文件夹
        String baseFilePath = RuoYiConfig.getUploadPath();
        String filePath1 = baseFilePath + filePath.replace("/profile/upload","");
        File file = new File(filePath1);
        if (file.exists()) {
            //删除
            boolean delete = file.delete();
        }
    }

    @Override
    public int uploadServerFile(Attachment attachment, String tableName) {
       //前端传过来 事前评估id 初始文件名 附件url 备注
        //从url中截取生成的新名字
        String substring = attachment.getAttachmentUrl().substring(attachment.getAttachmentUrl().lastIndexOf("/") + 1);
        attachment.setAttachmentName(substring);
        //设置附件类型
        attachment.setAttachmentType("1");
        attachment.setTableName(tableName);

        //重复上传进行替换
        Attachment att=new Attachment();
        att.setInfoDetailId(attachment.getInfoDetailId());
        att.setInfoId(attachment.getInfoId());
        List<Attachment> attachments=attachmentMapper.selectAttachmentList(att);
        if(attachments.size()>0){
            Attachment attach= attachments.get(0);
            //删除服务器附件
            this.deleteServerFile(attach.getAttachmentUrl());
            //删除数据库附件
            attachmentMapper.deleteAttachmentByAttachmentId(attach.getAttachmentId());
        }
        //设置创建人
        attachment.setCreateUser(SecurityUtils.getUserId());
        //创建时间
        attachment.setCreateTime(new Date());
        return attachmentMapper.insertAttachment(attachment);
    }

    @Override
    public void deleteAttachmentByAttachmentName(String attachmentName) {
         attachmentMapper.deleteAttachmentByAttachmentName(attachmentName);
    }
}
